package com.bluesky.plum.uimodels.standard.components.containers.form.commonForms;

import com.bluesky.plum.richdomain.RichDomainField;
import com.bluesky.plum.richdomain.RichDomainFields;
import com.bluesky.plum.uimodels.standard.FieldUIComponent;
import com.bluesky.plum.uimodels.standard.StandardUIComponentType;
import com.bluesky.plum.uimodels.standard.UIComponentBuilder;
import com.bluesky.plum.uimodels.standard.components.EventListener;
import com.bluesky.plum.uimodels.standard.components.containers.Panel;
import com.bluesky.plum.uimodels.standard.components.containers.form.Form;
import com.bluesky.plum.uimodels.standard.components.containers.window.Dialog;
import com.bluesky.plum.uimodels.standard.components.containers.window.ModalResult;
import com.bluesky.plum.uimodels.standard.components.containers.window.Window;

/**
 * create a dialog, list all fields and a save and cancel buttons.
 * 
 * @author xp
 * 
 */
public class EntityEditDialogBuilder {
	Dialog dialog;

	Form form;

	Object entity;
	
	
	
	protected UIComponentBuilder uiComponentBuilder;

	
	/**
	 * 
	 * @param entity non-null object
	 * @param uiComponentBuilder
	 */
	public EntityEditDialogBuilder(final Object entity,
			UIComponentBuilder uiComponentBuilder) {
		this.uiComponentBuilder = uiComponentBuilder;

		
		//entity must not be null
		if(entity==null)
			throw new RuntimeException("entity must not be null.");
		
		// show properties in the dialog and show the dialog;
		this.entity = entity;
		
		RichDomainFields rdfs = RichDomainFields.createFromEntity(entity);

		final FieldUIComponent[] uics = getUiComponentBuilder()
				.createComponents(
						rdfs.getFields().getList().toArray(
								new RichDomainField[] {}));

		form = (Form) getUiComponentBuilder().createNonFieldComponent(
				StandardUIComponentType.Form);

		form.setName("object editor form");
		// auto create label pair
		form.addFieldComponents(uics);

		form.addButton("OK", new EventListener() {
			@Override
			public void process() {
				// gather parameters from Form
				Object[] parameterValues = new Object[uics.length];
				for (int i = 0; i < uics.length; i++)
					parameterValues[i] = uics[i].getDomainFieldValue();

				

				form.writePojo(entity);
				// System.out.println("write entity:" + entity);
				// m.invoke(bo, parameterValues);
				
				dialog.setModalResult(ModalResult.OK);
				
				//close the dialog window
				dialog.setVisible(false);
			}
		});

		form.addButton("Cancel", new EventListener() {
			@Override
			public void process() {
				dialog.setModalResult(ModalResult.Cancel);
				
				// return
				dialog.setVisible(false);

			}
		});

	}

	public Dialog getDialog(Window parent, String title) {
		dialog = (Dialog) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Dialog);

		// create a dialog
		dialog.setParentWindow(parent);

		dialog.setTitle(title);

		final Panel contentPanel = (Panel) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Panel);
	

		contentPanel.addChildComponent(form);
		dialog.setContentPanel(contentPanel);

		return dialog;
	}

	public UIComponentBuilder getUiComponentBuilder() {
		return uiComponentBuilder;
	}

	public void setUiComponentBuilder(UIComponentBuilder uiComponentBuilder) {
		this.uiComponentBuilder = uiComponentBuilder;
	}

	
	public Object getEntity(){
		return entity;
	}
	
	
}
